<template>
  <div class="pagebody">
    <StickHead></StickHead>
    <div class="headtop bgfff text-al-center fz30">
      {{ pageid ? "企业信息" : "添加企业信息" }}
    </div>
    <div class="formbox bgfff">
      <div class="label fz22">
        <span class="fz18 colorF90000">*</span>企业名称
      </div>
      <div class="inputbox align-center mb25">
        <input
          type="text"
          class="input_text fz28 borderbox"
          placeholder="请输入企业名称"
          v-model="form.enterpriseName"
        />
      </div>
      <div class="label fz22"><span class="fz18 colorF90000">*</span>法人</div>
      <div class="inputbox align-center mb25">
        <input
          type="text"
          class="input_text fz28 borderbox"
          placeholder="请输入法人姓名"
          v-model="form.corporation"
        />
      </div>
      <div class="label fz22">
        <span class="fz18 colorF90000">*</span>联系人
      </div>
      <div class="inputbox align-center mb25">
        <input
          type="text"
          class="disabled input_text fz28 borderbox"
          disabled
          placeholder="请输入联系人姓名"
          v-model="form.contactPerson"
        />
      </div>
      <div class="label fz22">
        <span class="fz18 colorF90000">*</span>联系电话
      </div>
      <div class="inputbox align-center mb25">
        <input
          type="text"
          class="disabled input_text fz28 borderbox"
          placeholder="请输入联系电话"
          disabled
          v-model="form.contactPhone"
        />
      </div>
      <div class="label fz22">
        <span class="fz18 colorF90000">*</span>社会统一信用代码
      </div>
      <div class="inputbox align-center mb25">
        <input
          type="text"
          class="input_text fz28 borderbox"
          placeholder="请输入社会统一信用代码"
          v-model="form.creditCode"
        />
      </div>
      <div class="label fz22"><span class="fz18 colorF90000">*</span>地域</div>
      <div class="inputbox align-center mb25">
        <van-field
          readonly
          clickable
          label=""
          :value="dzvalue"
          placeholder="请选择地域"
          @click="dzshowPicker = true"
        />
        <van-popup v-model="dzshowPicker" round position="bottom">
          <van-picker
            show-toolbar
            :columns="dzentcolumns"
            @cancel="dzshowPicker = false"
            @confirm="dzonConfirm"
          />
        </van-popup>
      </div>
      <div class="label fz22">
        <span class="fz18 colorF90000">*</span>企业规模
      </div>
      <div class="inputbox align-center mb25">
        <van-field
          readonly
          clickable
          label=""
          :value="gmvalue"
          placeholder="请选择企业规模"
          @click="showPicker = true"
        />
        <van-popup v-model="showPicker" round position="bottom">
          <van-picker
            show-toolbar
            :columns="entcolumns"
            @cancel="showPicker = false"
            @confirm="onConfirm"
          />
        </van-popup>
      </div>

      <div class="label mt20 fz22">
        <span class="fz18 colorF90000">*</span>行业类型
      </div>
      <div class="inputbox align-center mb25">
        <van-field
          readonly
          clickable
          label=""
          :value="lxvalue"
          placeholder="请选择企业规模"
          @click="lxshowPicker = true"
        />
        <van-popup v-model="lxshowPicker" round position="bottom">
          <van-picker
            show-toolbar
            :columns="lxentcolumns"
            @cancel="lxshowPicker = false"
            @confirm="lxonConfirm"
          />
        </van-popup>
      </div>
      <div class="submit fz34" @click="submit">立即提交</div>
      <div class="deletes fz34" @click="deletes">删除</div>
    </div>

    <StickFoot :type="'myself'"></StickFoot>
  </div>
</template>

<script>
import StickHead from "@/layout/StickHead.vue";
import { Toast } from "vant";
import StickFoot from "@/layout/StickFoot.vue";
import {
  getDict,
  addenterprise,
  getenterpriseDetail,
  getDeptList,
  deleteenterprise,
  editenterprise,
} from "@/api/api";
export default {
  name: "addinformation",
  components: {
    StickHead,
    StickFoot,
  },
  data() {
    return {
      pageid: null,
      form: {
        enterpriseName: "",
        corporation: "",
        contactPerson: "",
        contactPhone: "",
        creditCode: "",
        enterpriseSize: "",
        industryType: "",
        areaCode: "",
      },
      gmvalue: "",
      showPicker: false,
      enterpriseList: [],
      entcolumns: [],
      lxvalue: "",
      lxshowPicker: false,
      lxenterpriseList: [],
      lxentcolumns: [],
      userinfo: {},
      dzvalue: "",
      dzshowPicker: false,
      dzenterpriseList: [],
      dzentcolumns: [],
    };
  },
  created() {
    if (this.$route.params.id) {
      this.pageid = this.$route.params.id;
    }
  },
  mounted() {
    this.getentList();
    this.getlxentList();
    this.getdzList();
    this.userinfo = JSON.parse(window.localStorage.getItem("userinfo"));
    this.form.contactPerson = this.userinfo.nickName;
    this.form.contactPhone = this.userinfo.phonenumber;
    if (this.pageid) {
      this.getent();
    }
  },
  methods: {
    getent() {
      getenterpriseDetail(this.pageid).then((response) => {
        this.form.enterpriseName = response.data.data.enterpriseName;
        this.form.corporation = response.data.data.corporation;
        this.form.contactPerson = response.data.data.contactPerson;
        this.form.contactPhone = response.data.data.contactPhone;
        this.form.creditCode = response.data.data.creditCode;
        this.form.industryType = response.data.data.industryType;
        this.form.enterpriseSize = response.data.data.enterpriseSize;
        setTimeout(() => {
          let enterpriseitem = this.enterpriseList.find(
            (item) => item.dictValue == response.data.data.enterpriseSize
          );
          let industryitem = this.lxenterpriseList.find(
            (item) => item.dictValue == response.data.data.industryType
          );
          let dzitem = this.dzenterpriseList.find(
            (item) => item.areaCode == response.data.data.areaCode
          );
          if (enterpriseitem) {
            this.gmvalue = enterpriseitem.dictLabel;
          }
          if (industryitem) {
            this.lxvalue = industryitem.dictLabel;
          }
          if (dzitem) {
            this.dzvalue = dzitem.deptName;
          }
        }, 200);
      });
    },
    submit() {
      let enterpriseitem = this.enterpriseList.find(
        (item) => item.dictLabel === this.gmvalue
      );
      let industryitem = this.lxenterpriseList.find(
        (item) => item.dictLabel === this.lxvalue
      );
      let dzitem = this.dzenterpriseList.find(
        (item) => item.deptName === this.dzvalue
      );
      if (enterpriseitem) {
        this.form.enterpriseSize = enterpriseitem.dictValue;
      }
      if (industryitem) {
        this.form.industryType = industryitem.dictValue;
      }
      if (dzitem) {
        this.form.areaCode = dzitem.areaCode;
      }
      if (this.rules(this.form)) {
        Toast.fail("请将信息填写完整");
        return;
      } else {
        if (this.pageid) {
          this.form.id = this.pageid;
          editenterprise(this.form).then((response) => {
            if (response.data.code === 200) {
              Toast.success("修改成功");
              this.$router.push({ name: "information" });
            } else {
            }
          });
        } else {
          addenterprise(this.form).then((response) => {
            if (response.data.code === 200) {
              Toast.success("新增成功");
              this.$router.push({ name: "information" });
            } else {
            }
          });
        }
      }
    },
    // 表单校验
    rules(obj) {
      for (let key in obj) {
        if (obj.hasOwnProperty(key)) {
          if (obj[key] === null || obj[key] === undefined || obj[key] === "") {
            return true;
          }
        }
      }
      return false;
    },
    deletes() {
      deleteenterprise(this.pageid).then((response) => {
        if (response.data.code === 200) {
          Toast.success("删除成功");
          this.$router.push({ name: "information" });
        } else {
          Toast.fail("删除失败");
        }
      });
    },
    onConfirm(value) {
      this.gmvalue = value;
      this.showPicker = false;
    },
    lxonConfirm(value) {
      this.lxvalue = value;
      this.lxshowPicker = false;
    },
    dzonConfirm(value) {
      this.dzvalue = value;
      this.dzshowPicker = false;
    },
    getentList() {
      let data = {
        dictType: "enterprise_size",
      };
      //获取字典数据
      getDict(data).then((response) => {
        this.enterpriseList = response.data.rows;
        this.enterpriseList.forEach((element) => {
          this.entcolumns.push(element.dictLabel);
        });
      });
    },
    getlxentList() {
      let data = {
        pageNum: 1,
        pageSize: 20,
        dictType: "industry_type",
      };
      //获取字典数据
      getDict(data).then((response) => {
        this.lxenterpriseList = response.data.rows;
        this.lxenterpriseList.forEach((element) => {
          this.lxentcolumns.push(element.dictLabel);
        });
      });
    },
    getdzList() {
      let data = {
        parentId: 210,
      };
      //获取字典数据
      getDeptList(data).then((response) => {
        this.dzenterpriseList = response.data.data;
        this.dzenterpriseList.forEach((element) => {
          this.dzentcolumns.push(element.deptName);
        });
      });
    },
  },
};
</script>

<style lang="scss">
::v-deep .van-cell {
  font-size: 0.24rem;
}
::v-deep .van-field__control {
  font-size: 0.48rem !important;
}
.pagebody {
  background-color: #f6f8fb;
  .headtop {
    line-height: 0.8rem;
    height: 0.8rem;
  }
  .formbox {
    border-top: 1px solid #666;
    padding: 0.2rem 0.2rem 2rem;
    .inputbox {
      height: 1rem;
      border-bottom: 1px solid #e5e5e5;
      .input_text {
        border-radius: 0.16rem;
        padding: 0 0.24rem;
        border: none;
        height: 0.8rem;
        width: -webkit-fill-available;
      }
      .disabled {
        background: #efefef !important;
      }
    }
    .submit {
      width: 100%;
      height: 0.8rem;
      line-height: 0.8rem;
      text-align: center;
      border: 2px solid #fff;
      color: #fff;
      border-radius: 0.7rem;
      background: #3399ff;
      margin-top: 0.2rem;
    }
    .deletes {
      width: 100%;
      height: 0.8rem;
      line-height: 0.8rem;
      text-align: center;
      border: 2px solid #fff;
      color: #fff;
      border-radius: 0.7rem;
      background: #e03f27;
      margin-top: 0.2rem;
    }
  }
}
</style>